RoomUtil = {
	
	ROLL_NORMAL_TEXTURE:'images/mouse/normal.png',
	ROLL_LEFTDOWN_TEXTURE:'images/mouse/leftdown.png',
	//1px：5mm。一个像素等于5mm。
	wallThick : 4,   //default 2
	wallHeight : 244,
	INIT_FLOOR_TEXTURE : "obj/wall/initfloor.jpg",
	INIT_WALL_TEXTURE : "obj/wall/wallpaper018.jpg",
	TEXTURE_WIDTH:64,
	TEXTURE_HEIGHT:64,
	CELL_SIZE:50,
	CELL2D_SIZE:20,
	PLANE2D_SIZE:2000,
	y3D:400,
	OFFSET_SIZE:{x : 210,
		y : 50},

transMouse22D:function(mouse){
    var mouse2D = new THREE.Vector3(0, 0, 0.5);
  if ( mouse.clientX!=null ) {
  
    mouse.preventDefault();
    mouse2D.x = ((mouse.clientX - RoomUtil.OFFSET_SIZE.x) / $('canvas').width()) * 2 - 1;
    mouse2D.y = -((mouse.clientY -RoomUtil.OFFSET_SIZE.y) / ($('canvas').height())) * 2 + 1;
  }else{
    mouse2D.copy(mouse);
  }
  return mouse2D;
},
		
// 		随机数有问题
	random : function() {
		var today = new Date();
		var date = today.getTime();
		// return Math.ceil(date)+Math.random(10);
		return Math.ceil(date);

	},
	/*
	 * 判断两个矩形是否相交或相邻或不交。
	 * 0：不交；1：相交；2：相邻。
	 */
	checkRectIntersectOrUnion : function(rectA, rectB) {

		/*
		 如果有坐标，方法更简单
		 矩形用的是最小点，最大点点对表示的   Rect(minPt,   maxPt)
		 rect1(minpt1,   maxpt1),   rect2(minpt2,   maxpt2)相交结果为
		 rect（minpt，maxpt）
		 minpt.x   =   max(   minpt1.x,   minpt2.x);
		 minpt.y   =   max(   minpt1.y,   minpt2.y);
		 maxpt.x   =   min   (maxpt1.x,   maxpt2.x);
		 maxpt.y   =   min   (maxpt1.y,   maxpt2.y);
		 如果   minpt.x   >   maxpt.x   ||   minpt.y   >   maxpt.y   那么表示交集为空，即不相交，否则就相交
		 */

		var min = {
			"x" : Math.max(rectA.position.x - rectA.size.x / 2, rectB.position.x - rectB.size.x / 2),
			"y" : Math.max(rectA.position.y - rectA.size.y / 2, rectB.position.y - rectB.size.y / 2)
		};
		var max = {
			"x" : Math.min(rectA.position.x + rectA.size.x / 2, rectB.position.x + rectB.size.x / 2),
			"y" : Math.min(rectA.position.y + rectA.size.y / 2, rectB.position.y + rectB.size.y / 2)
		};

		if(max.x == min.x && max.y == min.y) {
			// 	max.x==min.x&&max.y==min.y,说明两矩形在一个点上相交，在此也认为矩形不相邻。
			return 0;
		} else if(max.x == min.x || max.y == min.y) {
			return 2;
		} else if(min.x < max.x && min.y < max.y) {
			return 1;
		} else {
			return 0;
		}
	},
	/*
	 * rectA是被测线段。
	 * rectB是作为标尺的线段。
	 * return 数组的length=0，说明输入的两条线段不相交。
	 * return 数组的length=1，说明输入的两条线段重合。
	 * 
	 * 以下是测试代码：
	 *     	var rectA={"position":{"x":100,"y":-10},"size":{"x":80,"y":50}};
	 *		var rectB={"position":{"x":100,"y":0},"size":{"x":100,"y":20}};
	 *		console.warn(checkRoomHasWall(rectA,rectB));
	 */
	checkRoomHasWall : function(rectA, rectB) {
		var lA, lB;
		var rects = [];
		var c = null;

		if(rectA.position.z == rectB.position.z && 
		   rectA.position.x == rectB.position.x &&	
		   rectA.size.x == rectB.size.x &&
		   rectA.size.z == rectB.size.z) {
		   rects.push(rectA);
			return rects;
		} 
		else if(rectA.position.x   == rectB.position.x&&
			    rectA.position.z   == rectB.position.z){
			    	
			  if(rectA.gTypeId=='xwall'){
			  		lA = {
						"min" : Math.round(rectA.position.x - rectA.size.x / 2),
						"max" : Math.round(rectA.position.x + rectA.size.x / 2)
					};
					lB = {
						"min" : Math.round(rectB.position.x - rectB.size.x / 2),
						"max" : Math.round(rectB.position.x + rectB.size.x / 2)
					};
					c = {
						"c" : "z",
						"v" : rectA.position.z
					};
			  }	else if(rectA.gTypeId=='ywall'){
			  				lA = {
				"min" : Math.round(rectA.position.z - rectA.size.z / 2),
				"max" :Math.round(rectA.position.z + rectA.size.z / 2)
			};
			lB = {
				"min" : Math.round(rectB.position.z - rectB.size.z / 2),
				"max" : Math.round(rectB.position.z + rectB.size.z / 2)
			};
			c = {
				"c" : "x",
				"v" : rectA.position.x
			};
			  }	    	

		}
		
		else if(rectA.position.x   == rectB.position.x  ) {
			lA = {
				"min" : Math.round(rectA.position.z - rectA.size.z / 2),
				"max" :Math.round(rectA.position.z + rectA.size.z / 2)
			};
			lB = {
				"min" : Math.round(rectB.position.z - rectB.size.z / 2),
				"max" : Math.round(rectB.position.z + rectB.size.z / 2)
			};
			c = {
				"c" : "x",
				"v" : rectA.position.x
			};

		} else if(rectA.position.z   == rectB.position.z  ) {
			lA = {
				"min" : Math.round(rectA.position.x - rectA.size.x / 2),
				"max" : Math.round(rectA.position.x + rectA.size.x / 2)
			};
			lB = {
				"min" : Math.round(rectB.position.x - rectB.size.x / 2),
				"max" : Math.round(rectB.position.x + rectB.size.x / 2)
			};
			c = {
				"c" : "z",
				"v" : rectA.position.z
			};
		} else {
			return [];
		}
		var lines = checkLine(lA, lB);
		for(var i = 0; i < lines.length; i++) {
			var line = lines[i];
			if(c.c == "x") {
				var rect = {
					"position" : {
						"x" : c.v,
						"y" : RoomUtil.wallThick/2+RoomUtil.wallHeight/2,
						"z" : line.p
					},
					"size" : {
						"x" : RoomUtil.wallThick,
						"y" : RoomUtil.wallHeight,
						"z" : line.s
					},
					"gTypeId":"ywall",
					'on':line.on
				};
				rects.push(rect);
			} else if(c.c == "z") {
				var rect = {
					"position" : {
						"x" : line.p,
						"y" : RoomUtil.wallThick/2+RoomUtil.wallHeight/2,
						"z" : c.v
					},
					"size" : {
						"x" : line.s,
						"y" : RoomUtil.wallHeight,
						"z" : RoomUtil.wallThick
					},
					"gTypeId":"xwall",
					'on':line.on
				};
				rects.push(rect);
			}
		} ;
		return rects;
		function checkLine(lA, lB) {
			

			var list = [];
			// if(lA.min==lB.min&&lA.max==lB.max){
				// var l={
					// "p":(lA.max+lA.min)/2,
					// "s":Math.abs(lA.max-lA.min),
					// "on":"a"
				// }
				// list.push(l);
				// return [];
			// }
			var maxP = Math.min(lA.max, lB.max);
			var minP = Math.max(lA.min, lB.min);
			if(maxP <= minP) {
				//             	不相交
				return list;
			} else {
				if(lA.max >maxP) {
					var l = {
						"p" : (lA.max + maxP) / 2,
						"s" : (lA.max - maxP),
						"on" :"a"
					};
					list.push(l);
				}
				if(lA.min < minP) {
					var l = {
						"p" : ( minP + lA.min) / 2,
						"s" : ( minP - lA.min),
						"on" :"a"
					};
					list.push(l);
				}

				if(lB.max > maxP) {
					var l = {
						"p" : (lB.max + maxP) / 2,
						"s" : (lB.max - maxP),
						"on" :"b"
					};
					list.push(l);
				}
				if(lB.min < minP) {
					var l = {
						"p" : ( minP + lB.min) / 2,
						"s" : ( minP - lB.min),
						"on" :"b"
					};
					list.push(l);
				}
					// var l = {
						// "p" : (maxP+minP)/2 ,
						// "s" : ( maxP-minP),
						// "on" :"n"
					// };
					// list.push(l);
				return list;
			}
		};
		function transXYZ(rect){
				var tmpP=rect.position.y;
				var tmpS=rect.position.y;
				var r={position:{x:rect.position.x,y:rect.position.z,z:tmpP},
					   size:{x:rect.size.x,y:rect.size.z,z:tmpS}};
					   return r;
		}
	},
	
	getRoomTypeName:function(roomTypeId){
		switch(roomTypeId){
			case "1":
			return "卧室";
			case "2":
			return "客厅";
			case "3":
			return "卫生间";
			case "4":
			return "厨房";
			case "5":
			return "储藏室";
		}
	}
};
